namespace FIREFLYX {
    /** 太阳系 
     * 展示 transform parent 基本功能
    */
    export class SolarSystem implements IExample {
        public Init(canvas: HTMLCanvasElement) {
            let gl = canvas.getContext("webgl2");
            //app init
            App.init(gl.canvas as any);
            //简单观察相机
            let cVEnv = GenCamViewEnv();
            let cam = cVEnv.camera;
            let camHover = cVEnv.camHover;
            camHover.distance = 30;
            //light
            const light = GenMainLight(45);

            //创建一个太阳系
            //太阳、地球、月球
            let sun = GameObjectUtil.createPrimitive(GameObjectPrimitiveType.SPHERE);
            let earth = GameObjectUtil.createPrimitive(GameObjectPrimitiveType.SPHERE);
            let moon = GameObjectUtil.createPrimitive(GameObjectPrimitiveType.SPHERE);
            sun.name = "SUN";
            earth.name = "EARTH";
            moon.name = "MOON";
            moon.transform.parent = earth.transform;
            earth.transform.parent = sun.transform;

            sun.transform.localScale = new Vector3(3, 3, 3);
            // sun.transform.localRotation = Quaternion.RotationYawPitchRoll(-45 * toRadian, 0, 0);
            earth.transform.localScale = new Vector3(0.5, 0.5, 0.5);
            moon.transform.localScale = new Vector3(0.2, 0.2, 0.2);
            earth.transform.localPostion = new Vector3(5);
            moon.transform.localPostion = new Vector3(1);

            //attach ctr comp
            let srcomp = sun.addComponent<RotateAction>(RotateAction);
            srcomp.speed = 5;
            let ercomp = earth.addComponent<RotateAction>(RotateAction);
            ercomp.speed = 50;

            //test--
            GlobalEventManager.Instance.addListener("pointDown", (p) => { console.log(`pointDown ${p.x} ${p.y}`); }, this);
            GlobalEventManager.Instance.addListener("pointUp", (p) => { console.log(`pointUp ${p.x} ${p.y}`); }, this);
            GlobalEventManager.Instance.addListener("pointClick", (p) => { console.log(`pointClick ${p.x} ${p.y}`); }, this);
            // GlobalEventManager.Instance.addListener("pointMove", (p) => { console.log(`pointMove ${p.x} ${p.y}`); }, this);
            //------

        }

    }

    /** 旋转组件 */
    class RotateAction extends Script {
        private _speed = 10;
        private _angle = 0;
        private _axis = new Vector3(0, 1, 0);

        public get speed() { return this._speed; }
        public set speed(val) { this._speed = val; }

        public update(dt: number) {
            //
            const e = this.entity;
            if (!e) return;
            this._angle += this._speed * toRadian * dt;
            const t = e.transform;
            Quaternion.RotationAxisToRef(this._axis, this._angle, t.localRotation);
            t.localRotation = t.localRotation;
        }
    }
}